<?php
defined('OA_LIB_PATH') || define('OA_LIB_PATH', '/opt/op/OALib');
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once 'OaLogic.php';
require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';
require_once OPEN_LIB_PATH . '/open/models/OaAppServerModel.php';
require_once OPEN_LIB_PATH . '/open/models/OaCodeModel.php';
require_once OPEN_LIB_PATH . '/open/business-logic/OaSyncInfoLogic.php';
require_once OPEN_LIB_PATH . '/open/business-logic/service/OaServiceHelper.php';

class OaAppLogic extends OaLogic
{
	/**
	 * 将应用移到另外一个公司下
	 * @param int $appId
	 * @param int $newCompanyId
	 * @return boolean
	 */
    public static function moveAppToAnotherCompany($appId, $newCompanyId, &$rt)
    {
		//定义步骤
		$step = array( 
    					0=>array('修改应用表里公司ID', true ), 
    					1=>array('删除应用下的成员', true ),
    					2=>array('设置新公司的管理员为应用管理员', true ), 
    					3=>array('同步测试环境OAS', true ), 
    					4=>array('同步测式环境OA', true ), 
    					5=>array('同步正式环境OAS', true ), 
    					6=>array('同步正式环境OA', true )
					);
        $logger = self::getLogger();
        $appModel = new OaAppModel(OaAppModel::APP_CONFIG_ENV_PRODUCTION, $logger);
        $appServerModel = new OaAppServerModel(OaAppModel::APP_CONFIG_ENV_PRODUCTION, $logger);
        $app = $appModel->getApp($appId);
        if (!is_array($app)) {
            $logger->err("app not exists: app-$appId");
            return false;
        }
        
    	$companyModel = new OaCompanyModel($logger);
        $company = $companyModel->getCompany($newCompanyId);
        if (!is_array($company)) {
        	$logger->err("company not exists: company-$newCompanyId");
        	return false;
        }
		// 修改应用表里的公司ID
		$upRow = array(
        	'company_id' => $newCompanyId
        );
        if ($app['company_id']==OaAppModel::SDG_GAME_SP_ID || $newCompanyId==OaAppModel::SDG_GAME_SP_ID) {
        	if ($app['company_id']==OaAppModel::SDG_GAME_SP_ID) {
        		$upRow['is_sdg'] = OaAppModel::NOT_SDG_GAME;
        	} else {
        		$upRow['is_sdg'] = OaAppModel::IS_SDG_GAME;
        	}
        }
        if (!$appModel->modifyApp($appId, $upRow)) {
			$step[0][1] = false;
            $logger->err("modify app failed: app-$appId, old_company-{$app['company_id']}, new_company-$newCompanyId");
            return false;
        }         
        // 删除应用下的成员
        $appDevelopers = $appModel->getAppDevelopers($appId);
        foreach ($appDevelopers as $developer) {
            if (!$appModel->deleteDeveloper($appId, $developer['developer_id'])) {
				$step[1][1] = false;
                $logger->err("delete developer failed: app-$appId, developer-{$developer['developer_id']}");
                return false;
            }
        }
        
        // 设置新公司的管理员为应用管理员
        $companyAdmin = $companyModel->getCompanyAdmin($newCompanyId);
        if (is_array($companyAdmin)) {
        	if (!$appModel->addDeveloper($appId, $companyAdmin['pt'], OaAuthorityModel::ROLE_APP_ADMIN)) {
				$step[2][1] = false;
        		$logger->warning("add app admin failed: app-$appId, pt-{$companyAdmin['pt']}");
        	}
        } else {
        	$logger->warning("company no admin: company-$newCompanyId");
        }
                        
    	// 同步Code、OA、OAS库
        $code = new OaCodeModel($logger);
        if ($app['join_type'] != OaAppModel::APP_JOIN_FLOW_FAST_SINGLE && is_array($code->getApp($appId, OaAppModel::APP_CONFIG_ENV_TEST))) {
            if ( !OASApi::SPMigration($message, array( 'appId'=>$appId, 'spId'=>$app['company_id'], 'newspId'=>$newCompanyId ), OaAppModel::APP_CONFIG_ENV_TEST) )
            {//同步OAS
				$step[3][1] = false;
            	$logger->warning( "OASApi::SPMigration failed: app-$appId, env-2".$message );
            }
            
	        if (!OaSyncInfoLogic::syncAppInfo($appId, 'update', OaAppModel::APP_CONFIG_ENV_TEST)) {
				$step[4][1] = false;
	            $logger->warning("sync app info failed: app-$appId, env-1");
	        }
        }
        else
        {
        	$step[3][0] = false;
        	$step[4][0] = false;
        }
        
        if (is_array($code->getApp($appId, OaAppModel::APP_CONFIG_ENV_PRODUCTION))) {
            if ( !OASApi::SPMigration($message, array( 'appId'=>$appId, 'spId'=>$app['company_id'], 'newspId'=>$newCompanyId ), OaAppModel::APP_CONFIG_ENV_PRODUCTION) )
            {
				$step[5][1] = false;
            	$logger->warning( "OASApi::SPMigration failed: app-$appId, env-2".$message );
            }
	        if (!OaSyncInfoLogic::syncAppInfo($appId, 'update', OaAppModel::APP_CONFIG_ENV_PRODUCTION)) {
				$step[6][1] = false;
	            $logger->warning("sync app info failed: app-$appId, env-2");
	        }
        }
        else
        {
        	$step[5][0] = false;
        	$step[6][0] = false;
        }

        // 同步各大服务
        $appConfigTest = $appModel->getAppConfig($appId, null, OaAppModel::APP_CONFIG_ENV_TEST);
        $appConfigProduction = $appModel->getAppConfig($appId, null, OaAppModel::APP_CONFIG_ENV_PRODUCTION);
        if ($appConfigTest['service_pay_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updatePayService($appId, OaAppModel::APP_CONFIG_ENV_TEST)) {
        	$logger->warning("update pay service failed: app-$appId, env-1");
        	$step[7] = array( '重新开通测试环境支付服务' , false);
        }elseif ($appConfigTest['service_pay_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[7] = array( '重新开通测试环境支付服务' , true );
        
        if ($appConfigProduction['service_pay_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updatePayService($appId, OaAppModel::APP_CONFIG_ENV_PRODUCTION)) {
        	$logger->warning("update pay service failed: app-$appId, env-2");
        	$step[8] = array( '重新开通正式环境支付服务' , false);
        }elseif ($appConfigProduction['service_pay_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[8] = array( '重新开通正式环境支付服务' , true);
        
    	if ($appConfigTest['service_help_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updateHelpService($appId, OaAppModel::APP_CONFIG_ENV_TEST)) {
        	$logger->warning("update help service failed: app-$appId, env-1");
        	$step[9] = array( '重新开通测试环境彩虹帮助服务' , false);
        }elseif ($appConfigTest['service_help_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[9] = array( '重新开通测试环境彩虹帮助服务' , true);
        
        if ($appConfigProduction['service_help_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updateHelpService($appId, OaAppModel::APP_CONFIG_ENV_PRODUCTION)) {
        	$logger->warning("update help service failed: app-$appId, env-2");
        	$step[10] = array( '重新开通正式环境彩虹帮助服务' , false);
        }elseif ($appConfigProduction['service_help_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[10] = array( '重新开通正式环境彩虹帮助服务' , true);
        
    	if ($appConfigTest['service_bonus_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updateBonusService($appId, OaAppModel::APP_CONFIG_ENV_TEST)) {
        	$logger->warning("update bonus service failed: app-$appId, env-1");
        	$step[11] = array( '重新开通测试环境分红服务' , false);
        }elseif ($appConfigTest['service_bonus_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[11] = array( '重新开通测试环境分红服务' , true);
        
        if ($appConfigProduction['service_bonus_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updateBonusService($appId, OaAppModel::APP_CONFIG_ENV_PRODUCTION)) {
        	$logger->warning("update bonus service failed: app-$appId, env-2");
        	$step[12] = array( '重新开通正式环境分红服务' , false);
        }elseif ($appConfigProduction['service_bonus_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[12] = array( '重新开通正式环境分红服务' , true);
        
    	if ($appConfigTest['service_points_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updatePointsService($appId, OaAppModel::APP_CONFIG_ENV_TEST)) {
        	$logger->warning("update points service failed: app-$appId, env-1");
        	$step[13] = array( '重新开通测试环境积分服务' , false);
        }elseif ($appConfigTest['service_points_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[13] = array( '重新开通测试环境积分服务' , true);
        
        if ($appConfigProduction['service_points_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!OaServiceHelper::updatePointsService($appId, OaAppModel::APP_CONFIG_ENV_PRODUCTION)) {
        	$logger->warning("update points service failed: app-$appId, env-2");
        	$step[14] = array( '重新开通正式环境积分服务' , false);
        }elseif ($appConfigProduction['service_points_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[14] = array( '重新开通正式环境积分服务' , true);
        
		if ($appConfigTest['service_billing_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!SDOServiceBilling::modifyApp($message_bill, $app['id'], $app['name'], 'pre', $app['pay_type'], $newCompanyId) ){
        	$logger->warning("update bill service failed: app-$appId, env-1");
        	$step[15] = array( '重新开通测试环境计费服务' , false);
        }elseif ($appConfigTest['service_billing_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[15] = array( '重新开通测试环境计费服务' , true);   
             
		if ($appConfigProduction['service_billing_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED && 
        	!SDOServiceBilling::modifyApp($message_bill, $app['id'], $app['name'], 'online', $app['pay_type'], $newCompanyId) ){
        	$logger->warning("update bill service failed: app-$appId, env-2");
        	$step[16] = array( '重新开通正式环境计费服务' , false);
        }elseif ($appConfigProduction['service_billing_status'] == OaAppModel::APP_SERVICE_STATUS_OPENED) $step[16] = array( '重新开通正式环境计费服务' , true); 

        $rt = $step;
        
        foreach ( $step as $s ){
        	if ( !$s[1] ) return false;
        }
        return true;
    }
}